﻿Imports System.Web.UI
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Configuration
Imports System.Net.Mail

Partial Public Class Wfrm_Consultar_OC
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ViewState("usu") = Session("usuario")
        End If

    End Sub

    Protected Sub btnBuscaSol_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuscaSol.Click
        Carga_Grilla_OC()
        btnImprimir.Visible = False
        btnImprimir.UpdateAfterCallBack = True
    End Sub
    Sub Carga_Grilla_OC()
        Try


            Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
            Dim rp As New DotNetResponse.SQLPersistence
            rp.StoredProcedure = "SP_OC_GET_ORDEN_COMPRA"

            If txtNsolicitud.Text.Trim.Length > 0 Then
                rp.AddParametro(Val(txtNsolicitud.Text))
            Else
                rp.AddParametro(Nothing)
            End If

            If txtNsolicitud0.Text.Trim.Length > 0 Then
                rp.AddParametro(Val(txtNsolicitud0.Text))
            Else
                rp.AddParametro(Nothing)
            End If

            If txtStartDate.Text.Trim.Length > 0 Then
                rp.AddParametro(Convert.ToDateTime(txtStartDate.Text).ToString("yyyyMMdd"))
            Else
                rp.AddParametro(Nothing)
            End If

            If txtEndDate.Text.Trim.Length > 0 Then
                rp.AddParametro(Convert.ToDateTime(txtEndDate.Text).ToString("yyyyMMdd"))
            Else
                rp.AddParametro(Nothing)
            End If

            If txtStartCodigo.Text.Trim.Length > 0 Then
                rp.AddParametro(txtStartCodigo.Text.Trim)
            Else
                rp.AddParametro(Nothing)
            End If

            If txtEndCodigo.Text.Trim.Length > 0 Then
                rp.AddParametro(txtEndCodigo.Text.Trim)
            Else
                rp.AddParametro(Nothing)
            End If

            If cmb_estado_solicitud.Text.Trim.Length > 0 Then
                rp.AddParametro(cmb_estado_solicitud.SelectedValue)
            Else
                rp.AddParametro(Nothing)
            End If

            exec.ExecuteDataTable(rp)
            If rp.Errores Then
                Anthem.Manager.AddScriptForClientSideEval("alert('" & rp.MensajeError & "')")
            End If

            Grilla_OC_enc.DataSource = rp.DtTable
            Grilla_OC_enc.Visible = True
            Panel1.Visible = True
            Panel1.UpdateAfterCallBack = True
            Grilla_OC_enc.DataBind()
            Grilla_OC_enc.UpdateAfterCallBack = True

        Catch ex As Exception


        End Try

    End Sub

    Protected Sub lnk_despachar_Click(ByVal sender As Object, ByVal e As EventArgs)
        lbl_nro_oc.Text = DirectCast(sender.parent.parent, DataGridItem).Cells(2).Text
        lbl_nro_oc.UpdateAfterCallBack = True

        lbl_fecha.Text = DirectCast(sender.parent.parent, DataGridItem).Cells(4).Text
        lbl_fecha.UpdateAfterCallBack = True

        lbl_proveedor.Text = DirectCast(sender.parent.parent, DataGridItem).Cells(3).Text
        lbl_proveedor.UpdateAfterCallBack = True

        Carga_Productos(DirectCast(sender.parent.parent, DataGridItem).Cells(2).Text)

        Panel1.Visible = True
        lblSolicitud7.Visible = True
        lblSolicitud6.Visible = True
        lbl_proveedor.Visible = True
        lbl_nro_oc.Visible = True
        'lblSolicitud2.Visible = True
        lblSolicitud1.Visible = True

        Panel1.UpdateAfterCallBack = True
        lblSolicitud7.UpdateAfterCallBack = True
        lblSolicitud6.UpdateAfterCallBack = True
        lbl_proveedor.UpdateAfterCallBack = True
        lbl_nro_oc.UpdateAfterCallBack = True
        'lblSolicitud2.UpdateAfterCallBack = True
        lblSolicitud1.UpdateAfterCallBack = True

        btnImprimir.Visible = True
        btnImprimir.UpdateAfterCallBack = True

    End Sub
    Sub Carga_Productos(ByVal id_oc As String)
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GET_PRODUCTOS_ORDEN_COMPRA_SOL"
        rp.AddParametro(id_oc)
        exec.ExecuteDataTable(rp)

        If Not rp.TieneDatos Then
            If Anthem.Manager.IsCallBack Then
                Anthem.Manager.AddScriptForClientSideEval("alert('" & "No existen productos en la Orden de Compra" & "')")
            Else
                Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & "No existen productos en la Orden de Compra" & "')</script>")
            End If
        End If

        DataGrid4.DataSource = rp.DtTable
        DataGrid4.DataBind()
        DataGrid4.UpdateAfterCallBack = True
    End Sub


    Protected Sub btnImprimir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnImprimir.Click
        Dim rp As New Rpt_OrdenCompra

        Dim _CRConnectionInfo As ConnectionInfo

        _CRConnectionInfo = New ConnectionInfo()
        _CRConnectionInfo.ServerName = ConfigurationManager.AppSettings("cnn_server") '"application.dyndns-web.com"
        _CRConnectionInfo.DatabaseName = ConfigurationManager.AppSettings("cnn_database") '"comprasFarm"
        _CRConnectionInfo.UserID = ConfigurationManager.AppSettings("cnn_userid") '"lilas"
        _CRConnectionInfo.Password = ConfigurationManager.AppSettings("cnn_password") '"lilas"

        Dim tables As Tables
        tables = rp.Database.Tables
        For i = 0 To tables.Count - 1
            Dim tableLogonInfo As TableLogOnInfo = tables(i).LogOnInfo
            tableLogonInfo.ConnectionInfo = _CRConnectionInfo
            tables(i).ApplyLogOnInfo(tableLogonInfo)
        Next

        rp.SetParameterValue(0, lbl_nro_oc.Text)
        ' rp.SetParameterValue(1, lbl_nro_oc.Text)


        'Dim tables2 As Tables
        'tables2 = rp.Subreports(0).Database.Tables
        'For i = 0 To tables2.Count - 1
        '    Dim tableLogonInfo As TableLogOnInfo = tables2(i).LogOnInfo
        '    tableLogonInfo.ConnectionInfo = _CRConnectionInfo
        '    tables2(i).ApplyLogOnInfo(tableLogonInfo)
        'Next

        'rp.Subreports(0).SetParameterValue("@OC_1", ViewState("id_oc"))
        'rp.Subreports(0).SetParameterValue(0, 17)
        'DESPACHO_DB(lbl_nro_oc.Text)

        rp.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Page.Response, True, "OrdenCompra_" & lbl_nro_oc.Text & ".pdf")
        'rp.ExportToDisk (ExportFormatType.PortableDocFormat ,"")

    End Sub

    
End Class